﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CADDemo2025
{
    public class Jig : DrawJig
    {
        private Point3d _point;
        private List<Entity> _ents;
        private List<Entity> _ents1;
        private Action<Point3d, List<Entity>, List<Entity>> _func;
        private string _message;
        private Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
        public Jig(Point3d point, Action<Point3d, List<Entity>, List<Entity>> func, string message = "")
        {
            _point = point;
            _ents = new List<Entity>();
            _ents1 = new List<Entity>();
            _func = func;
            _message = message;
            if (ed.Drag(this).Status == PromptStatus.OK)
            {
                EmEntityMethod.AddEntities(_ents);
            }
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            PromptPointResult psr = prompts.AcquirePoint(new JigPromptPointOptions(_message));
            if (psr.Status == PromptStatus.OK)
            {
                Point3d poNow = psr.Value;
                if (poNow != _point)
                {
                    _ents.Clear();
                    _ents1.Clear();
                    _func(poNow, _ents, _ents1);
                    return SamplerStatus.OK;
                }
                else
                {
                    return SamplerStatus.NoChange;
                }
            }
            return SamplerStatus.Cancel;
        }

        protected override bool WorldDraw(Autodesk.AutoCAD.GraphicsInterface.WorldDraw draw)
        {
            _ents.ForEach(x => draw.Geometry.Draw(x));
            _ents1.ForEach(x => draw.Geometry.Draw(x));
            return true;
        }
    }

}
